package com.axon.components.controls
{
	import com.axon.components.containers.AxPanel;
	import com.axon.components.containers.AxTitleWindowSizeable;
	
	import flash.display.DisplayObjectContainer;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.ui.Keyboard;
	
	import mx.controls.ComboBox;
	import mx.core.Container;
	import mx.events.FlexEvent;
	
	import org.restfulx.models.RxModel;

	[Style(name="axBackgroundColor",			type="uint", format="Color", inherit="yes")]
	[Style(name="axBackgroundColorReadOnly", 	type="uint", format="Color", inherit="yes")]

	public class AxComboBox extends ComboBox
	{
		//////////////////////////////////////////////////////////////////////////////////////////////////////
		
		public function AxComboBox()
		{
			super();
			addEventListener(FlexEvent.CREATION_COMPLETE, onCreationComplete);
		}
		
		//////////////////////////////////////////////////////////////////////////////////////////////////////
		
		override public function set enabled(value: Boolean): void
		{
			super.enabled = value;
			refreshStyle();
		}

		//////////////////////////////////////////////////////////////////////////////////////////////////////
		
		protected function onCreationComplete( event: FlexEvent ): void
		{
			refreshStyle();
		}
		
		//////////////////////////////////////////////////////////////////////////////////////////////////////

		protected function refreshStyle(): void
		{
			setStyle("fillColors", (enabled) 	? [getStyle("axBackgroundColor"), getStyle("axBackgroundColor")] 
												: [getStyle("axBackgroundColorReadOnly"), getStyle("axBackgroundColorReadOnly")] );
		}		

		//////////////////////////////////////////////////////////////////////////////////////////////////////
		
    	override protected function keyDownHandler(event: KeyboardEvent):void
    	{
			var theParent: DisplayObjectContainer = parent;

    		switch (event.charCode) {
    			
    			case Keyboard.ENTER:

					while ((theParent != null) && !(theParent is AxPanel) && !(theParent is AxTitleWindowSizeable))
						theParent = theParent.parent;
						
					if ( (theParent != null)&& (Container(theParent).defaultButton) )
						Container(theParent).defaultButton.dispatchEvent(new MouseEvent(MouseEvent.CLICK))
					
					break;
				
				case Keyboard.ESCAPE:
				
					while ((theParent != null) && !(theParent is AxPanel) && !(theParent is AxTitleWindowSizeable))
						theParent = theParent.parent;
						
					if (theParent != null) 
						theParent.dispatchEvent(event.clone());
						
					break;
					
				default:
				
					super.keyDownHandler(event);
				
    		}
    	}

		//////////////////////////////////////////////////////////////////////////////////////////////////////
		
		public function selectItemById(id: String): Object
		{
			selectedItem = getItemById(id);
			return selectedItem;
		}
		//////////////////////////////////////////////////////////////////////////////////////////////////////
		
		public function getItemById(id: String): Object
		{
			var result: Object = null;
			
			for (var i: Number = 0; i < collection.length; i++) 
				if ( ( collection[i] is RxModel ) && (RxModel(collection[i]).id == id) ) {
					result = collection[i];
					break; }
					
			return result;
		}
	}
}